import java.awt.desktop.SystemEventListener;
import java.util.Scanner;

public class test {
    public static void main1(String[] args) {
        int a = 10;
        int b = 20;
        //方法的调用是需要开辟内存的
        int c = sum(a, b);
        System.out.println(c);
    }

    public static int sum(int a, int b) {
        return a + b;
    }

    public static void main2(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 5; i++) {
            sum += fac(i);
        }
        System.out.println("sum=" + sum);
    }

    public static int fac(int n) {
        System.out.println("计算n的阶乘中n！=" + n);
        int ret = 1;
        for (int i = 1; i <= n; i++) {
            ret *= i;
        }
        return ret;
    }

    // 方法的重载
    public static void main3(String[] args) {
        int x = add(1, 2);
        double y = add(1.5, 2.5);
        double z = add(1.5, 2.5, 3.5);
        System.out.println(x);
        System.out.println(y);
        System.out.println(z);
    }

    public static int add(int a, int b) {
        return a + b;
    }

    public static double add(double a, double b) {
        return a + b;
    }

    public static double add(double a, double b, double c) {
        return a + b + c;
    }

    // 以下代码是作业

    // 题1 创建方法求两个数的最大值max2，随后再写一个求3个数的最大值的函数max3。
    // 要求：在max3这个函数中，调用max2函数，来实现3个数的最大值计算

    public static void main4(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int b = in.nextInt();
        int c = in.nextInt();
        int max = compThree(a, b, c);
        System.out.println("最大值是" + max);
    }

    public static int compTwo(int a, int b) {
        if (a >= b) {
            return a;
        } else {
            return b;
        }
    }

    public static int compThree(int a, int b, int c) {
        int max = compTwo(a, b);
        if (c >= max) {
            return c;
        } else {
            return max;
        }
    }

    //题2 求N的阶乘
    public static void main5(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int ret = 1;
        for (int i = 1; i <= n; i++) {
            ret *= i;
        }
        System.out.println("n的阶乘是" + ret);
    }

    //题3 求阶乘和 求1！+2！+3！+4！+........+n!的和
    public static void main6(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            int tmp = fac1(i);
            sum += tmp;
        }
        System.out.println("n的阶乘和是" + sum);
    }

    public static int fac1(int n) {
        int ret = 1;
        for (int i = 1; i <= n; i++) {
            ret *= i;
        }
        return ret;
    }

    // 题4 斐波那契数列，求斐波那契数列的第n项。(迭代实现)
    public static void main7(String[] args) {

    }

    // 题6 求最大值方法的重载 在同一个类中定义多个方法：要求不仅可以求2个整数的最大值，还可以求3个小数的最大值？
    public static void main8(String[] args) {
        Scanner in = new Scanner(System.in);
        double a = in.nextDouble();
        double b = in.nextDouble();
        double c = in.nextDouble();
        double max = compThree(a, b, c);
        System.out.println("最大值是" + max);
    }

    public static double compTwo(double a, double b) {
        if (a >= b) {
            return a;
        } else {
            return b;
        }
    }

    public static double compThree(double a, double b, double c) {
        double max = compTwo(a, b);
        if (max >= c) {
            return max;
        } else {
            return c;
        }
    }

    // 递归求n的阶乘
    public static void main9(String[] args) {
        int n = 5;
        int ret = factor(5);
        System.out.println("ret=" + ret);
    }

    public static int factor(int a) {
        if (a == 1) {
            return 1;
        }
        return a * factor(a - 1);
    }

    // 按顺序打印一个数字的每一位 1234 打印出 1 2 3 4
    public static void main10(String[] args) {
        int a = 1234;
        print(a);
    }

    public static void print(int a) {
        if (a / 10 == 0) {
            System.out.println(a);
            return;
            // 一定要有return
        }
        print(a / 10);
        System.out.println(a % 10);
    }

    // 递归求 1+2+3+4...+10
    public static void main11(String[] args) {
        int n = 10;
        int sum = sum_1(10);
        System.out.println("sum=" + sum);
    }

    public static int sum_1(int a) {
        if (a == 1) {
            return a;
        }
        int sum = 0;
        sum = a + sum_1(a - 1);
        return sum;
    }

    // 递归 输入一个非负整数，返回组成它的数字之和， 输入1279 返回 1+2+7+9=19
    public static void main12(String[] args) {
        int n = 1279;
        int sum = sum_3(n);
        System.out.println(sum);
    }

    public static int sum_3(int n) {
        if (n / 10 == 0) {
            return n;
        }
        return n % 10 + sum_3(n / 10);
    }

    // 求第n项费波契数
    public static void main13(String[] args) {
        int n = 10;
        int fib_num = fib(10);
        System.out.println("第十项斐波那契数位 " + fib_num);
    }

    public static int fib(int n) {
        if (n == 1 | n == 2) {
            return 1;
        }
        return fib(n - 1) + fib(n - 2);
    }

    // 循环的方法来求第n项斐波那契数
    public static void main(String[] args) {
        int n = 10;
        int fib_num = fib_1(n);
        System.out.println("第十项斐波那契数位 " + fib_num);
    }

    public static int fib_1(int n) {
        int last2 = 1;// 前两个数中的第二个
        int last1 = 1;//前两个数中的第一个

        int cur = 0;
        for (int i = 3; i <= n; i++) {
            cur = last2 + last1;
            last1 = last2;
            last2 = cur;
        }
        return cur;
    }
}

